'********************************************************************
' 
'  file:  AccountManagementSample.vb
'  Account Management Sample Call (VB)
'
'  (C) Copyright 2010, Diomede Corporation
'  All rights reserved
' 
'  Use, modification, and distribution is subject to   
'  the New BSD License (See accompanying file license.txt).
' 
'********************************************************************

Imports data.diomedestorage.com
Imports Diomede.SDK.Enums

Namespace Diomede.SDK.ExampleVB
    Public Class AccountManagementSample
        Public Sub start()
            Try

                Console.WriteLine(vbCrLf)
                Console.WriteLine(vbCrLf)

                CreateAccountSample()
                SetAccountInfoSample()
                GetAccountInfoSample()
                AddEmailAddressSample()
                SetPrimaryEmailAddressSample()
                DeleteAccountInfoSample()
                GetEmailAddressSample()
                GetEmailAddressListSample()
                RemoveEmailAddressSample()
                SearchLoginLogs()

            Catch ex As Exception

                Console.WriteLine(ex.Message)
            End Try
            Utils.WriteMessage("Press any key...")
            Console.ReadKey()
        End Sub

#Region "Examples"

        Shared username As String = String.Format("user_{0}", DateTime.Now.Ticks)
        Shared password As String = "password914"
        Shared email As String = String.Format("{0}@example.com", username)

        Shared Sub CreateAccountSample()
            Dim session As ISession = New Session()
            Dim accountManager As IAccountManagement = CType(session, IAccountManagement)

            If accountManager.Create(username, password, email).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to create account.")
            Else
                Console.WriteLine("Account successfully created.")
            End If
        End Sub


        Shared Sub SetAccountInfoSample()
            Dim session As ISession = New Session()
            If session.Login(username, password).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to login.")
            End If

            Dim accountManager As IAccountManagement = CType(session, IAccountManagement)

            'create userinfo
            Dim userInfo As New UserInfo
            userInfo.firstName = "Melvin"
            userInfo.lastName = "Sembrano"
            userInfo.companyName = "MS Company"
            userInfo.websiteUrl = "www.melvin.com"
            userInfo.cardName = "AMelvin Sembrano"
            userInfo.cardNumber = "1234567890123456"
            userInfo.cardCvv2 = "023"
            userInfo.cardAddress1 = "123 Address 1-A"
            userInfo.cardAddress2 = "321 Address 2-B"
            userInfo.cardCity = "Cebu"
            userInfo.cardState = "Cebu P"
            userInfo.cardCountry = "63"
            userInfo.cardZip = "6000"

            If accountManager.UpdateAccountInfo(userInfo).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to update account information.")
            Else
                Console.WriteLine("Successfully update the user info.")
            End If
            session.Logout()
        End Sub

        Shared Sub GetAccountInfoSample()
            Dim session As ISession = New Session()
            If session.Login(username, password).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to login.")
            End If

            Dim accountManager As IAccountManagement = CType(session, IAccountManagement)

            Dim result As ISessionExecResult = accountManager.GetAccountInfo()
            If result.Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to get user's account information.")
            End If

            Dim userInfo As UserInfo = result.UserInfo
            Console.WriteLine(String.Format("{0} {1}'s information.", userInfo.firstName, userInfo.lastName))
        End Sub

        Shared Sub DeleteAccountInfoSample()
            Dim session As ISession = New Session
            If session.Login(username, password).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to login.")
            End If

            Dim accountManager As IAccountManagement = CType(session, IAccountManagement)

            'delete the current billing info
            If accountManager.DeleteAccountInfo(UserInfoType.allBillingInfo).Status <> ExecutionResults.Success Then
                Throw New Exception("Fail to delete billing information.")
            Else
                Console.WriteLine("Successfully deleted the account billing information.")
            End If
        End Sub

        Shared Sub AddEmailAddressSample()
            Dim session As ISession = New Session()
            If session.Login(username, password).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to login.")
            End If

            Dim accountManager As IAccountManagement = CType(session, IAccountManagement)

            If accountManager.AddEmail(String.Format("{0}_second@sample.com", username)).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to add email address.")
            Else
                Console.WriteLine("Successfully added email address.")
            End If
        End Sub

        Shared Sub SetPrimaryEmailAddressSample()
            Dim session As ISession = New Session()
            If session.Login(username, password).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to login.")
            End If

            Dim accountManager As IAccountManagement = CType(session, IAccountManagement)

            If accountManager.AddEmail(String.Format("{0}_newprimary@sample.com", username)).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to set new primary email address.")
            Else
                Console.WriteLine("Successfully set primary email address.")
            End If
        End Sub

        Private Shared Sub GetEmailAddressSample()
            Dim session As ISession = New Session()
            If session.Login(username, password).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to login.")
            End If

            Dim accountManager As IAccountManagement = TryCast(session, IAccountManagement)

            If accountManager.GetEmailAddresses().Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to retrieve email addresses.")
            Else
                Console.WriteLine("Successfully retrieved email addresses.")
            End If
        End Sub

        Private Shared Sub GetEmailAddressListSample()
            Dim session As ISession = New Session()
            If session.Login(username, password).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to login.")
            End If

            Dim accountManager As IAccountManagement = TryCast(session, IAccountManagement)

            If accountManager.GetEmailAddressList() Is Nothing Then
                Throw New Exception("Fails to retrieve email addresses list.")
            Else
                Console.WriteLine("Successfully retrieved email addresses list.")
            End If
        End Sub


        Private Shared Sub RemoveEmailAddressSample()
            Dim session As ISession = New Session()
            If session.Login(username, password).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to login.")
            End If

            Dim accountManager As IAccountManagement = TryCast(session, IAccountManagement)

            If accountManager.RemoveEmailAddress(String.Format("{0}_second@sample.com", username)).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to remove email address.")
            Else
                Console.WriteLine("Successfully removed email address.")
            End If
        End Sub

        Private Shared Sub SearchLoginLogs()
            Dim session As ISession = New Session()
            If session.Login(username, password).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to login.")
            End If

            Dim logManager As ILogsManagement = TryCast(session, ILogsManagement)

            If logManager.SearchLoginLogs(Nothing, Nothing, String.Empty, 0, 100).Status <> ExecutionResults.Success Then
                Throw New Exception("Search on login logs fails.")
            Else
                Console.WriteLine("Search on login logs successful.")
            End If
        End Sub

#End Region

    End Class
End Namespace